package mbp.gui.panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import mbp.core.Contract;
import mbp.gui.base.IListPanel;
import mbp.message.contract;

public class ContractListPanel extends JPanel  implements IListPanel
{
	private JTable cntrTable;
	private DefaultTableModel cntrModel;
	
	private List<contract> cntriList;
	private List<Contract> cntrList;
	
	private JScrollPane scrollPane;

	/**
	 * Create the panel.
	 */
	public ContractListPanel() 
	{
		setLayout(new BorderLayout(0, 0));
		scrollPane = new JScrollPane();
		add(scrollPane);
		
		cntrTable = new JTable(){
			@Override
			public boolean isCellEditable(int row, int column) {
		    // set table column uneditable
		    return false;
			}
		  };
		  
		  cntrTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		  cntrTable.setFillsViewportHeight(true);
		  scrollPane.setViewportView(cntrTable);
			
		  cntrTable.setModel(new DefaultTableModel(
					new Object[][] {
					},
					new String[] {
							"State", "Service type", "Server", "UUID"
					}
				));
		  cntrModel = (DefaultTableModel) cntrTable.getModel();
		  
		  //podwojne klikniecie na tabele i pobranie indeksu zaznaczonego wiersza
		  cntrTable.addMouseListener(new MouseAdapter(){
			  public void mouseClicked(MouseEvent e){
				  if (e.getClickCount() == 2){
					  processDoubleClick(cntrTable.getSelectedRow());
				  }
			  }
		  } );
		  
		  MyTableCellRenderer mtcr = new MyTableCellRenderer();
		  cntrTable.setDefaultRenderer(Object.class,mtcr);
		  
		  cntrTable.getColumnModel().getColumn(0).setMinWidth(100);
		  cntrTable.getColumnModel().getColumn(0).setMaxWidth(100);
		  cntrTable.getColumnModel().getColumn(1).setMinWidth(100);
		  cntrTable.getColumnModel().getColumn(1).setMaxWidth(100);
		  cntrTable.getColumnModel().getColumn(2).setMinWidth(100);
		  cntrTable.getColumnModel().getColumn(2).setMaxWidth(100);


	}
	
	private void reloadInfo()
	{
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				cntrModel.setRowCount(0);
				
				for(int i=0; i<cntriList.size(); i++)
				{
					//RcModel.setRowCount(i);
					String state = cntriList.get(i).state;
					String type = cntriList.get(i).type;
					String server = cntriList.get(i).server;
					String uuid = cntriList.get(i).uuid;
					cntrModel.insertRow(i, new Object[]{state, type, server, uuid});
				}
				
			}
		});
	}
	
	private void reloadObj()
	{
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	public void processDoubleClick(int index)
	{
		System.out.println(" double click: selected row: " + cntrTable.getSelectedRow());
	}

	@Override
	public <contract> void reloadListInfo(List<contract> infoList) {
		this.cntriList = (List<mbp.message.contract>) infoList;
		this.cntrList = null;
		reloadInfo();
		
	}

	@Override
	public <E> void reloadListObj(List<E> objList) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void reloadList() {
		// TODO Auto-generated method stub
		
	}
	
	public class MyTableCellRenderer extends DefaultTableCellRenderer
	{
		public Component getTableCellRendererComponent( JTable table, 
				Object value, 
				boolean isSelected, 
				boolean hasFocus,
				int row,
				int column )
		{
			Component c = super.getTableCellRendererComponent(table, value,
					isSelected, hasFocus,
					row, column);
			if(column == 0)
			{
				String svalue = (String) value;
				if(svalue.compareTo("NEW")==0)
				{
					c.setBackground(Color.LIGHT_GRAY);
				}
				
				if(svalue.compareTo("REALIZE")==0)
				{
					c.setBackground(Color.YELLOW);
				}
				
				if(svalue.compareTo("PRODUCE")==0)
				{
					c.setBackground(Color.ORANGE);
				}
				
				if(svalue.compareTo("CLOSED")==0)
				{
					c.setBackground(Color.GREEN);
				}
				
				if(svalue.compareTo("BROKEN")==0)
				{
					c.setBackground(Color.RED);
				}
				
				if(svalue.compareTo("REJECTED")==0)
				{
					c.setBackground(Color.RED);
				}
				
				if(svalue.compareTo("FROZEN")==0)
				{
					c.setBackground(Color.LIGHT_GRAY);
				}
			}
			else
			{
				c.setBackground(Color.WHITE);
			}
			
			return c;
					
		}
	}

}
